
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="设备id:" prop="deviceId">
          <el-input v-model="formData.deviceId" :clearable="true"  placeholder="请输入设备id" />
       </el-form-item>
        <el-form-item label="温度(°C):" prop="temperature">
          <el-input-number v-model="formData.temperature" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="CO浓度(%):" prop="coConcentration">
          <el-input-number v-model="formData.coConcentration" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="烟雾指数(ppm):" prop="smokeIndex">
          <el-input-number v-model="formData.smokeIndex" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="红外测温:" prop="infraredTemperature">
          <el-input-number v-model="formData.infraredTemperature" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="湿度:" prop="humidity">
          <el-input-number v-model="formData.humidity" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="摄像头捕捉范围内最高温:" prop="sonytem">
          <el-input-number v-model="formData.sonytem" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="氨气浓度:" prop="NH3">
          <el-input v-model.number="formData.NH3" :clearable="true" placeholder="请输入" />
       </el-form-item>
        <el-form-item label="警告标志位 abcd:" prop="warn">
          <el-input v-model="formData.warn" :clearable="true"  placeholder="请输入警告标志位 abcd" />
       </el-form-item>
        <el-form-item label="公司:" prop="companyName">
          <el-input v-model="formData.companyName" :clearable="true"  placeholder="请输入公司" />
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createDeviceData,
  updateDeviceData,
  findDeviceData
} from '@/api/device/deviceData'

defineOptions({
    name: 'DeviceDataForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const formData = ref({
            deviceId: '',
            temperature: 0,
            coConcentration: 0,
            smokeIndex: 0,
            infraredTemperature: 0,
            humidity: 0,
            sonytem: 0,
            NH3: undefined,
            warn: '',
            companyName: '',
        })
// 验证规则
const rule = reactive({
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findDeviceData({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createDeviceData(formData.value)
               break
             case 'update':
               res = await updateDeviceData(formData.value)
               break
             default:
               res = await createDeviceData(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
